package bb.lanxing.activity.sport;

import android.content.res.ColorStateList;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.mylhyl.circledialog.CircleDialog;
import com.mylhyl.circledialog.view.listener.OnButtonClickListener;

import bb.lanxing.R;
import bb.lanxing.activity.base.BaseActivity;
import bb.lanxing.manager.CadenceTrainingManager;
import bb.lanxing.manager.SharedManager;
import bb.lanxing.util.ui.DrawableUtils;

public class CadenceTrainingActivity extends BaseActivity implements View.OnClickListener {
    private TextView tvCadence;
    private ImageButton minusBtn;
    private ImageButton plusBtn;
    private TextView switchBtn;

    private int trainingCadence = SharedManager.getInstance().getPersonalCadence();

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_cadence_training);

        ImageView back = findViewById(R.id.back);
        back.setOnClickListener(v -> onBackPressed());

        ImageView help = findViewById(R.id.help);
        help.setOnClickListener(view -> new CircleDialog.Builder().setTitle(getString(R.string.cadence_training_help_title))
                .setText(getString(R.string.sport_cadence_training_help))
                .setPositive(getString(R.string.dialog_btn_known), (OnButtonClickListener) null)
                .show(getSupportFragmentManager()));

        tvCadence = findViewById(R.id.cadence);
        minusBtn = findViewById(R.id.minus);
        plusBtn = findViewById(R.id.plus);
        switchBtn = findViewById(R.id.switchBtn);

        ColorStateList colorStateList = getResources().getColorStateList(R.color.cadence_training_btn_selector);
        DrawableUtils.setTint(minusBtn, colorStateList);
        DrawableUtils.setTint(plusBtn, colorStateList);
        tvCadence.setText(String.valueOf(trainingCadence));
        switchBtn.setText(CadenceTrainingManager.isTraining() ? R.string.cadence_training_stop : R.string.cadence_training_start);
        switchBtn.setBackgroundResource(CadenceTrainingManager.isTraining() ? R.drawable.red_round_btn_bg : R.drawable.blue_round_btn_bg);
        checkBtnState();
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.minus) {
            changeCadence(trainingCadence - 1);
        } else if (id == R.id.plus) {
            changeCadence(trainingCadence + 1);
        } else if (id == R.id.switchBtn) {
            switchTraining();
        }
    }

    private void changeCadence(int i) {
        if (i > 200 || i < 20) {
            return;
        }
        trainingCadence = i;
        tvCadence.setText(String.valueOf(i));
        CadenceTrainingManager.getInstance().changeCadence(i);
        checkBtnState();
    }

    private void checkBtnState() {
        minusBtn.setEnabled(trainingCadence > 20);
        plusBtn.setEnabled(trainingCadence < 200);
    }

    private void switchTraining() {
        if (CadenceTrainingManager.isTraining()) {
            CadenceTrainingManager.getInstance().stop();
            switchBtn.setBackgroundResource(R.drawable.blue_round_btn_bg);
            switchBtn.setText(R.string.cadence_training_start);
        } else {
            CadenceTrainingManager.getInstance().start(this, trainingCadence);
            switchBtn.setBackgroundResource(R.drawable.red_round_btn_bg);
            switchBtn.setText(R.string.cadence_training_stop);
        }
    }

    @Override
    public void onDestroy() {
        if (!CadenceTrainingManager.isTraining()) {
            CadenceTrainingManager.release();
        }
        super.onDestroy();
    }
}